package model.location;

import model.Coordinate;
import model.Dimension;
import model.OnScreen;
import net.sourceforge.zbar.Symbol;

import com.google.zxing.Result;
import com.google.zxing.ResultPoint;

public class ReferencePointFactory 
{	
	public static ReferencePoint createReferencePoint(Result result)
	{
		ResultPoint[] resultPoints = result.getResultPoints();		
		
		float width, height;
		
		width  = Math.abs(resultPoints[resultPoints.length - 1].getX() - resultPoints[0].getX());
		height = Math.abs(resultPoints[resultPoints.length - 1].getY() - resultPoints[0].getY());

		Dimension dimension = new Dimension(width, height);
		
		/**
		 * returns upperleft corner of QR code.
		 * 
		 * length - 2 returns 1 for 2D barcode and 0 for 1D barcode.
		 */
		float xCorrection = OnScreen.getInstance().getXCorrection();
		float yCorrection = OnScreen.getInstance().getYCorrection();
		
		Coordinate coordinate = new Coordinate(resultPoints[resultPoints.length - 2].getX() / xCorrection, resultPoints[resultPoints.length - 2].getY() * yCorrection);
		
		return new ReferencePoint(result.toString(), new ScreenArea(coordinate, dimension));
	}
	
	public static ReferencePoint createReferencePoint(Symbol symbol)
	{
		int[] bounds = symbol.getBounds();
		
		Coordinate coordinate = new Coordinate((float) bounds[0], (float) bounds[1]);
		Dimension dimension = new Dimension((float) bounds[2], (float) bounds[3]);
		
		return new ReferencePoint(symbol.getData(),  new ScreenArea(coordinate, dimension));
	}
}

